package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.itheima.dao.OrderSettingDao;
import com.itheima.entity.Result;
import com.itheima.pojo.OrderSetting;
import com.itheima.service.OrderSettingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

@Service(version = "1.0.0", interfaceClass = OrderSettingService.class)
@Transactional
public class OrderSettingServiceImpl implements OrderSettingService {

    @Autowired
    private OrderSettingDao orderSettingDao;

    @Override
    public void add(ArrayList<OrderSetting> orderSettings) {
           /*this.leftobj = [
                        { date: 1, number: 120, reservations: 1 },
                        { date: 3, number: 120, reservations: 1 },
                        { date: 4, number: 120, reservations: 120 },
                        { date: 6, number: 120, reservations: 1 },
                        { date: 8, number: 120, reservations: 1 }
                    ];*/
        for (OrderSetting orderSetting : orderSettings) {
            OrderSetting dborderSetting = orderSettingDao.findOrderSettingByDate(orderSetting);
            if (dborderSetting == null) {
                orderSettingDao.add(orderSetting);
            } else {
                orderSettingDao.update(orderSetting);
            }
        }
    }

    @Override
    public Result findByMonth(String month) {

          /*this.leftobj = [
                        { date: 1, number: 120, reservations: 1 },
                        { date: 3, number: 120, reservations: 1 },
                        { date: 4, number: 120, reservations: 120 },
                        { date: 6, number: 120, reservations: 1 },
                        { date: 8, number: 120, reservations: 1 }
                    ];*/
        String start = month + "-01";
        String end = month + "-31";
        List<OrderSetting> orderSettingList = orderSettingDao.findBetWeenDate(start, end);
        ArrayList<Map> list = new ArrayList<>();
        for (OrderSetting orderSetting : orderSettingList) {
            Map<String, Integer> map = new HashMap<>();
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(orderSetting.getOrderDate());
            map.put("date", calendar.get(Calendar.DAY_OF_MONTH));
            map.put("number", orderSetting.getNumber());
            map.put("reservations", orderSetting.getReservations());
            list.add(map);
        }
        return Result.success(list);
    }

    @Override
    public Result updateNumber(String date, Integer number) throws ParseException {
        OrderSetting orderSetting = orderSettingDao.findDate(date);
        if (orderSetting != null) {
            orderSettingDao.updateNumber(date, number);
        } else {
            OrderSetting neworderSetting = new OrderSetting();
            neworderSetting.setOrderDate(new SimpleDateFormat("yyyy-MM-dd").parse(date));
            neworderSetting.setReservations(0);
            neworderSetting.setNumber(number);
            orderSettingDao.add(neworderSetting);
        }

        return null;
    }


}
